

#include "xgui_config.h"

#include "utf8_encoding.h"

GUI_RAM_CODE
static int utf8_get_size(uint8_t * utf8)
{
    if(utf8[0] < 0x80){
        return 1;
    }

    if(utf8[0] >= 0xC0 && utf8[0] < 0xE0){
        return 2;
    }

    if(utf8[0] >= 0xE0 && utf8[0] < 0xF0){
        return 3;
    }

    return 1; 
}

GUI_RAM_CODE
int utf8_decode( const uint8_t *utf8, uint16_t len_of_utf8, uint16_t* unicode )
{
    uint8_t * putf8 = (uint8_t*)utf8;
    int utf8_size = 0; 
    uint16_t temp;

    utf8_size = utf8_get_size(putf8);

    if( utf8_size == 1)
    {
        *unicode = *putf8;
    }
    else if( utf8_size == 2 )
    {
        temp = *putf8 & 0x1f;
        temp = (uint16_t)(*(putf8 + 1) & 0x3F) + (temp << 6);
        *unicode = temp;
    }
    else if( utf8_size == 3 )
    {
        temp = *putf8 & 0x0f;
        temp = (uint16_t)(*(putf8+1) & 0x3F) + (temp << 6);
        temp = (uint16_t)(*(putf8+2) & 0x3F) + (temp << 6);
        *unicode = temp;
    }

    
    return utf8_size; 
}
